package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabItemDictRefer;
import com.ysd.lis.mapper.lab.LabItemDictReferMapper;
import com.ysd.lis.service.lab.LabItemDictReferService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 报告项目参考范围 服务实现类
 * </p>
 *
 * @author bai
 * @since 2024-01-22
 */
@Service
public class LabItemDictReferServiceImpl extends ServiceImpl<LabItemDictReferMapper, LabItemDictRefer> implements LabItemDictReferService {

    @Autowired
    LabItemDictReferMapper itemDictReferMapper;
    private SysUser sysUser;

    @Override
    public Result findItemDictRefer(LabItemDictRefer labItemDictRefer) {
        if (ToolsUtils.isEmpty(labItemDictRefer)) {
            return Result.fail("参数不可为空");
        }
        if (ToolsUtils.isEmpty(labItemDictRefer.getItemNo())) {
            return Result.fail("报告项目不可为空");
        }
        SysUser sysUser = RedisUserManager.getUser();
        MPJLambdaWrapper<LabItemDictRefer> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabItemDictRefer.class)
                .selectAs("LID", LabItemDict::getItemNa, LabItemDictRefer::getItemNa)
                .leftJoin(LabItemDict.class, "LID",p->p.eq(LabItemDict::getItemNo, LabItemDictRefer::getItemNo).eq(LabItemDict::getRptGroup,LabItemDictRefer::getRptGroup).eq(LabItemDict::getDelFlag,0));
        if (ToolsUtils.isNotEmpty(sysUser)) {
            labItemDictRefer.setOrgId(sysUser.getOrgId());
            lambdaQueryWrapper.eq(LabItemDictRefer::getOrgId, labItemDictRefer.getOrgId());
        }

        lambdaQueryWrapper
                .eq(LabItemDictRefer::getItemNo, labItemDictRefer.getItemNo())
                .eq(LabItemDictRefer::getRptGroup, labItemDictRefer.getRptGroup())
                .eq(LabItemDictRefer::getDelFlag, 0);

        lambdaQueryWrapper.orderByAsc(LabItemDictRefer::getSeq);
        List<LabItemDictRefer> list = itemDictReferMapper.selectList(lambdaQueryWrapper);
        return Result.succ(list);
    }

    @Override
    public Result addItemDictRefer(LabItemDictRefer labItemDictRefer) {
        if (ToolsUtils.isEmpty(labItemDictRefer)) {
            return Result.fail("入参异常");
        }
        SysUser sysUser = RedisUserManager.getUser();
        labItemDictRefer.setOrgId(sysUser.getOrgId());
        labItemDictRefer.setIsEnable("1");
        if (ToolsUtils.isNotEmpty(labItemDictRefer.getFactorDescribe())) {
            String factorDescribe = labItemDictRefer.getFactorDescribe();
            labItemDictRefer.setFactorDescribe(factorDescribe + " )");
        }
        Boolean save = this.save(labItemDictRefer);
        if (save) {
            return Result.succ(1, "添加成功！", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result editItemDictRefer(LabItemDictRefer labItemDictRefer) {
        if (ToolsUtils.isNotEmpty(labItemDictRefer.getFactorDescribe())) {
            String factorDescribe = labItemDictRefer.getFactorDescribe();
            labItemDictRefer.setFactorDescribe(factorDescribe + " )");
        }
        if (ToolsUtils.isEmpty(labItemDictRefer)) {
            return Result.fail("入参异常");
        }
        Boolean save = this.updateById(labItemDictRefer);
        if (save) {
            return Result.succ(1, "修改成功！", null);
        } else {
            return Result.fail("修改失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteItemDictRefer(LabItemDictRefer labItemDictRefer) {
        if (ToolsUtils.isEmpty(labItemDictRefer)) {
            return Result.fail("入参不可为空！");
        }
        //是否需要判断有没有相关业务正在使用这个规则，先走流程。此处需要确认
        int delete = itemDictReferMapper.deleteById(labItemDictRefer.getId());
        if (delete > 0) {
            return Result.succ(1, "删除成功！", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
